class Solution {
public:
    int findNext(int n)
    {
        int next = 0;
        while(n != 0)
        {
            int s =  n%10;
            next += (s * s);
            n/=10;
        }        
        return next;
    }
    bool isHappy(int n) {
        int slow = findNext(n);
        int fast = findNext(findNext(n));
        while(fast != slow)
        {
            slow = findNext(slow);
            fast = findNext(findNext(fast));
            //cout<<slow<<" "<<fast<<endl;
        }
        if(slow == 1) return true;
        else return false;
    }
};
